<?php

/* 用户会员中心类 */
class service_user extends gdl_components_baseservice
{

	/* 所有错误提示信息 */
	public $error ='';
	public $ob_kqMember_model;
	public $ob_agkUnits_model;
	
 
	
	public function __construct(){
		$this->ob_kqMember_model =  new model_attendance_member();
		$this->ob_agkUnits_model =  new model_lhbase_units();
		
		
	}

	/*
	*@func	getMemberUnitsRelation 获取当前平台的组织架构和考勤用户的关联数组
	*@param	$platformId 平台ID
	*@param	$unitid 组织架构ID
	*@param	$type 返回类型 1 树形组织架构返回 2不树形返回
	*@param	$child 默认子集为_child 可修改
	*@param	$title 模糊查询部门名
	*@result 返回数组
	*/	
	
	public function getMemberUnitsRelation($platformId,$adminUserId = 0,$type=1,$child="_child",$title="",$unitIdR=0){

        //获取该平台的组织架构
        $units = [];
        $MemberInfo = [];
        $globals = new service_globals();
        $aa='lg';
//        if ($unitIdR){
//            $unitid = [$unitIdR];
//        }else{
//
//        }
        $appadminuser = new model_lhbase_appadminuser();
        $unitid = $appadminuser->operating($adminUserId,0,$platformId);
        $UnitsRelation = $this->ob_agkUnits_model->getMenuSpecial($aa,$platformId,$unitid,$title);
        if (!empty($UnitsRelation)){
            //处理数据
            foreach ($UnitsRelation as $key=>$value){
                $units[$key]['hasChildren'] = $value['hasChildren'];
                $units[$key]['id'] = $value['id'];
                $units[$key]['level'] = $value['level'];
                $units[$key]['pid'] = $value['pid'];
                $units[$key]['title'] = $value['title'];
            }
            if ($type==1)$units = $this->list_to_tree($units,'id','pid',$child,0,$platformId);
        }

//		$result = serialize($UnitsRelation);
		return ['relation'=>$units,'members'=>$MemberInfo];
	}

    /**
     * 节点遍历
     *
     * @param        $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int    $root
     *
     * @return array
     */
    private function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0,$platformId=0) {
        // 创建Tree
        $tree = [];
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = [];
            foreach ($list as $key => $data) {
                if ($data instanceof \think\Model) {
                    $list[$key] = $data->toArray();
                }
                $refer[$data[$pk]] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                if (!isset($list[$key][$child])) {
                    $list[$key][$child] = [];
                }
                $parentId = $data[$pid];
                $member = [];
                $members = $this->ob_kqMember_model->select(" unitId = {$list[$key]['id']} and platformId=$platformId and status=2")->items;
                if ($members){
                    foreach ($members as $keys=>$value){
                        $member[] = [
                            'userName'=>$value['userName'],
                            'realName'=>$value['realName'],
                            'unitId'=>$value['unitId'],
                            'id'=>$value['id']
                        ];
                    }
                }
                $list[$key]['member'] = $member;
                if ($root == $parentId) {
                    $tree[] = & $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = & $refer[$parentId];
                        $parent[$child][] = & $list[$key];
                    }
                }
            }
        }

        return $tree;
    }

    /*
        *@func	getUnitsRelation 获取当前平台集合下的所有平台ID
        *@param	$unitids 组织架构IDs
        *@result 返回数组
        */

    public function getUnitsRelation($units,$unitids, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0){

        //获取该平台的组织架构 $units
        // 创建Tree
        $tree = [];


        return $tree;
    }

}	
